#!/bin/bash
#
# passphraser: turn a passphrase (even a bad one like a verse from a well-known
# song) into a good password.
#
# WIP, inspired by miscellaneous posts in diaspora and gnusocial. No warranty
# of usage, not even that will work in your system.
#
# A few remarks:
#
#  - This program makes a sha224 hash as the final password. Assuming sha224
#    doesn't break (i.e. nobody finds collisions, etc), and the service you
#    use has a decent char limit for passwords, this should be OK. If your 
#    character limit is lower, then you should probably try a password manager
#    instead.
#  - Passwords here are deterministic, and many-to-one due to the process of
#    simplification used. IMO, this is a trade-off: less chance for typos but
#    a slightly higher chance of collision. You can change this easily below.
#  - Ideal usage: use the password you generated here to unlock your password
#    manager database. And then use that to generate your other 2000 passwords.
#
# Copyright 2018 - kzimmermann <https://notabug.org/kzimmermann>
#
# This program is free software licensed under the terms and conditions of the
# GNU GPL v3. See LICENCE for more information.
#

printf "Enter a phrase you know well: "

read phrase

if [[ -z $(which xsel) ]]
then

    printf "$phrase" | 
        tr " " "\n" |
        sort |
        tr "\n" "m" | # "m" is arbitrary.
        tr [A-Z] [a-z] |
        tr -dc [:alnum:] |
        base64 |
        sha224sum |
        cut -d " " -f 1

        echo "This is your suggested password."

else

    printf "$phrase" | 
        tr " " "\n" |
        sort |
        tr "\n" "m" | # "m" is arbitrary.
        tr [A-Z] [a-z] |
        tr -dc [:alnum:] |
        base64 |
        sha224sum |
        cut -d " " -f 1 |
        xsel -ib

    echo "Password copied to clipboard."

fi
